<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-02-11 21:37:40
 * Desc:
 */

namespace app\api\controller;

use app\base\model\user\User;
use app\base\model\user\UserWx;
use app\base\model\user\UserFriend;
use think\Request;
use app\core\log\Log4dd;

class UserController extends ApiBaseController
{
    protected $beforeActionList = [
        'checkLogin',
    ];


    public function getUserInfo()
    {

        $data = User::getUserPublicInfo($this->user_id);

        $data['is_bind'] = UserWx::MiniProgramIsBind($this->user_id) ? 1 : 0;

        if (in_array($this->user_id, array_column(User::TEST_USER_ARR, 'user_id'))) {
            $data['is_bind'] = 1;
        }

        Log4dd::info('获取用户信息', ['user_id' =>$this->user_id, 'data' => $data]);

        return $this->successReturn($data);

    }

    public function bindWxFriend()
    {
        $refer_code = input('refer_code/s', '');
        Log4dd::info('绑定微信好友信息,请求参数', [
            'user_id' => $this->user_id,
            'request_params' => Request::instance()->param(false)
        ]);

        if ($refer_code) {
            $refer_user_id = User::getUserIdByReferCode($refer_code);
            Log4dd::info('绑定微信好友信息,查询信息', [
                'user_id' => $this->user_id,
                'refer_code' => $refer_code,
                'refer_user_id' => $refer_user_id,
            ]);

            if ($refer_user_id && $refer_user_id != $this->user_id) {
                UserFriend::bindFriendByFriendUserId($this->user_id, $refer_user_id, UserFriend::SOURCE_MINI_BE_INVITE_ACCESS_WISH);
                UserFriend::bindFriendByFriendUserId($refer_user_id, $this->user_id, UserFriend::SOURCE_MINI_INVITE_ACCESS_WISH);
            }
        }

        return $this->successReturn();
    }

}